package com.bw.services.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bw.services.R
import com.bw.services.adapter.Car_ManageAdapter.ViewHolder
import com.bw.services.entity.CarParkingEntity

class Car_ParkingAdapter(private val itemLIst:List<CarParkingEntity>): RecyclerView.Adapter<Car_ParkingAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tv_parking_number: TextView = view.findViewById(R.id.tv_parking_number)
        val tv_parking_status: TextView = view.findViewById(R.id.tv_parking_status)
        val tv_parking_area: TextView = view.findViewById(R.id.tv_parking_area)
        val tv_parking_type: TextView = view.findViewById(R.id.tv_parking_type)
        val tv_parking_person: TextView = view.findViewById(R.id.tv_parking_person)
        val tv_parking_card: TextView = view.findViewById(R.id.tv_parking_card)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_car_parking, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int =itemLIst.size

    private var onItemClickListener: ((Int) -> Unit)? = null
    fun setOnItemClickListener(listener: (Int) -> Unit) {
        onItemClickListener = listener
    }

    private var onStatusClickListener: ((Int, CarParkingEntity) -> Unit)? = null
    fun setOnStatusClickListener(listener: (Int, CarParkingEntity) -> Unit) {
        onStatusClickListener = listener
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = itemLIst[position]
        // 车位编号
        if (!item.spaceNumber.isNullOrEmpty()) {
            holder.tv_parking_number.visibility = View.VISIBLE
            holder.tv_parking_number.text = item.spaceNumber
        } else {
            holder.tv_parking_number.visibility = View.GONE
        }
        // 状态
        if (!item.spaceStatus.isNullOrEmpty()) {
            holder.tv_parking_status.visibility = View.VISIBLE
            holder.tv_parking_status.text = item.spaceStatus
            when (item.spaceStatus) {
                "空闲" -> holder.tv_parking_status.setTextColor(0xFF4CAF50.toInt()) // 绿色
                "已占用" -> holder.tv_parking_status.setTextColor(0xFF2196F3.toInt()) // 蓝色
                "维护中" -> holder.tv_parking_status.setTextColor(0xFFFFC107.toInt()) // 黄色
                else -> holder.tv_parking_status.setTextColor(0xFF888888.toInt())
            }
            // 添加状态点击事件
            holder.tv_parking_status.setOnClickListener {
                onStatusClickListener?.invoke(position, item)
            }
        } else {
            holder.tv_parking_status.visibility = View.GONE
        }
        // 所属区域
        if (!item.areaName.isNullOrEmpty()) {
            holder.tv_parking_area.visibility = View.VISIBLE
            holder.tv_parking_area.text = "所属区域：${item.areaName}"
        } else {
            holder.tv_parking_area.visibility = View.GONE
        }
        // 车位类型
        if (!item.spaceType.isNullOrEmpty()) {
            holder.tv_parking_type.visibility = View.VISIBLE
            holder.tv_parking_type.text = "车位类型：${item.spaceType}"
        } else {
            holder.tv_parking_type.visibility = View.GONE
        }
        // 使用人
        if (!item.userName.isNullOrEmpty()) {
            holder.tv_parking_person.visibility = View.VISIBLE
            holder.tv_parking_person.text = "使用人：${item.userName}"
        } else {
            holder.tv_parking_person.visibility = View.GONE
        }
        // 车牌号
        if (!item.licensePlate.isNullOrEmpty()) {
            holder.tv_parking_card.visibility = View.VISIBLE
            holder.tv_parking_card.text = "车牌号：${item.licensePlate}"
        } else {
            holder.tv_parking_card.visibility = View.GONE
        }
        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(position)
        }
    }

}